<?php
namespace app\admin\model;

use think\Exception;
use think\facade\Db;
use think\facade\Request;
use think\Model;
//分类页
class Gcategory extends Model{
    protected $pk='cate_id';
    //cate  分类列表sql语句
    public static function cateList($where){
        $cate=Gcategory::field(true)->order('sort_order asc')
            ->where($where)
            ->select();
        return $cate;

    }
    //查询最后一级分类不跳页
     public static function cateNext(){
     $parentAllid=Gcategory::distinct(true)->column("parent_id");
     return $parentAllid;
         }

     //获取器效果转图片输出  图片死路径
    public static function   getXiaoguoImgAttr($v,$data){
        if ($data['xiaoguo']==1){
            return "<img src='/static/images/√.jpg' width='70' height='50'/>";
        }else{
            return "<img src='/static/images/X.jpg'  width='70' height='50' />";
        }
    }

    //cate 添加分类
     public static function   cateAddInfo($post){
        try{
            $res=Gcategory::insert($post);
        }catch (Exception $e){
            $res=false;
        }
           return $res;
         }

     //cate  修改分类
     public static function   cateEditInfo($param){
        if (Request::isPost()){
            try{
                $res=Gcategory::update($param);
            }catch (Exception $e){
                $res=false;
            }
            return $res;
        }else{
           $find=Gcategory::find($param['cate_id']);
           return $find;
        }

         }

     //cate  删除分类(有子分类的和商品相关联的不可以删除)
     public static function   cateDelInfo($get){
        try{
            $good= $list=\app\admin\model\Goods::distinct(true)->column("cate_id");
            $parent_id=Gcategory::distinct(true)->column('parent_id');
            if (!in_array($get['id'],$good) && !in_array($get['id'],$parent_id)){
                $res=Gcategory::destroy($get['id']);
                $msg="删除成功";
            }else{
                $res=false;
                $msg='删除失败(商品列表中含有此分类商品或此分类包含子分类)';
            }
        }catch (Exception $e){
            $res=false;
            $msg='删除失败';
        }
        return [$msg,$res];




     }








}